#include<bits/stdc++.h>
using namespace std;
#include<string.h>


int main(){
    int n;
    cin>>n;//操作次数

    string str;
    cin>>str;//初始字符串
    char arr[1000] = {'0'};//控制缓冲区
    while(n--){
        cin>>arr;
        if(arr[0] == '1'){
            //在文档末尾接入字符串
           // string tmp = str;
            string my_end;
            cin>>my_end;
            str+=my_end;
            cout<<str<<endl;
        }else if(arr[0] == '2'){
            //截取字符串
            //string tmp2 = str;
            int left,right;
            cin>>left>>right;
            string ret = str.substr(left,right);
            str = ret;
            cout<<str<<endl;
        }else if(arr[0] == '3'){
            //插入片段,在n的前面插入
            //string tmp3 = str;
            int n;
            cin>>n;
            string ret;
            cin>>ret;
            str.insert(n,ret);
            cout<<str<<endl;
        }else if(arr[0] == '4'){
            //查找子串
            //string tmp4 = str;
            string ret;
            cin>>ret;
            int n = str.find(ret);
            if(n>=0){
                cout<<n<<endl;
            }else{
                cout<<-1<<endl;
            }
        }
    }

    return 0;
}